function initSupplierCategory() {
	$("#create-category").button().click(function() {
		addOrEditSupCateDialog(ACTION_ADD);
	});

	$(".editBtn").button().click(function() {
		$("#supplier_category_name").val($(this).attr("cateName"));
		addOrEditSupCateDialog(ACTION_EDIT, $(this).attr("cate"));
	});

	$(".delBtn").button().click(function() {
		deleteSupplierCategory($(this).attr("cate"));
	});

	$("#delete-category").button().click(function() {
		deleteDialog(SUPPLIER_CATEGORY,"checkKey");
	});
}

function addOrEditSupCateDialog(action, updateId) {
	var name = $("#supplier_category_name"), allFields = $([]).add(name), tips = $(".validateTips");

	$("#dialog-form-category").css("display", "inline");
	$("#dialog-form-category").dialog({
		height : 230,
		width : 350,
		modal : true,
		resizable : false,
		buttons : {
			"确定" : function() {
				var bValid = true;
				allFields.removeClass("ui-state-error");

				bValid = bValid && checkLength(name, "类别", 1, 16, tips);

				if (bValid) {
					if (action == ACTION_ADD) {
						insertSupplierCategory(name.val());
						$(this).dialog("close");
					} else {
						updateSupplierCategory(updateId, name.val());
						$(this).dialog("close");
					}
				}
			},
			"取消" : function() {
				$(this).dialog("close");
			}
		},
		close : function() {
			allFields.val("").removeClass("ui-state-error");
		}
	});
}

function insertSupplierCategory(cateName) {
	var params = {
		name : cateName
	}
	mask.open(true);
	ajaxPost(website + "boSupplierCategory/insert", params, function(result) {
		$("#dialog-message").html(result);
		$("#dialog-message").dialog({
			modal : true,
			resizable : false,
			buttons : {
				Ok : function() {
					showSupplierCategory();
					$(this).dialog("close");
				}
			}
		});
		mask.close();
	}, "html");
}

function updateSupplierCategory(updateId, cateName) {
	var params = {
		id : updateId,
		name : cateName
	}
	mask.open(true);
	ajaxPost(website + "boSupplierCategory/update", params, function(result) {
		$("#dialog-message").html(result);
		$("#dialog-message").dialog({
			modal : true,
			resizable : false,
			buttons : {
				Ok : function() {
					showSupplierCategory();
					$(this).dialog("close");
				}
			}
		});
		mask.close();
	}, "html");
}

function showSupplierCategory() {
	mask.open(true);
	ajaxGet(website + "boSupplierCategory/show", null, function(result) {
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initSupplierCategory();
		mask.close();
	}, "html");
}

function deleteSupplierCategory(id) {
	$("#dialog-confirm").css("display", "inline");
	$("#dialog-confirm").dialog(
			{
				resizable : false,
				height : 140,
				modal : true,
				buttons : {
					"确定删除" : function() {
						var website = $("#website").val();
						var params = {
							cateid : id
						}
						mask.open(true);
						ajaxPost(website + "boSupplierCategory/delete", params,
								function(result) {
									$("#dialog-message").html(result);
									$("#dialog-message").dialog({
										modal : true,
										resizable : false,
										buttons : {
											Ok : function() {
												showSupplierCategory();
												$(this).dialog("close");
											}
										}
									});
									mask.close();
								}, "html");
						$(this).dialog("close");
					},
					"取消" : function() {
						$(this).dialog("close");
					}
				}
			});
}

